// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0


import Auth from '@aws-amplify/auth';
import { I18n, Logger } from '@aws-amplify/core';

// Logger for Utils
const logger = new Logger('Utils');

//api name
export const API_NAME = "ids";

//Limits
const stringMax = 30;
const latLimits = 90;
const lngLimits = 180;
const topicMax = 128;
const amtMin = 1;
const amtMax = 100; 
const intvlMin = 1;
const durationMin = 1;
const durationMax = 604800;
const charSetMax = 128;
const lngthMin = 1;
const lngthMax = 36;

//Error strings
const getErrors = (field: string, error: string) => {

  let ERRORS: { [key: string]: { [key: string]: string } } = {
    name: {
      length: `${I18n.get("string.max.length.error")} ${stringMax}`,
      chars: I18n.get("name.error")
    },
    general: {
      range: I18n.get("range.error"),
      required: I18n.get("required.error"),
      type: I18n.get("invalid.type")
    },
    lat: {
      min: `${I18n.get("min.error")} -${latLimits}`,
      max: `${I18n.get("max.error")} ${latLimits}`
    },
    long: {
      min: `${I18n.get("min.error")} -${lngLimits}`,
      max: `${I18n.get("max.error")} ${lngLimits}`
    },
    topic: {
      chars: I18n.get("topic.error"),
      length: `${I18n.get("string.max.length.error")} ${topicMax}`
    },
    amount: {
      min: `${I18n.get("min.error")} ${amtMin}`,
      max: `${I18n.get("max.error")} ${amtMax}`
    },
    interval: {
      min: `${I18n.get("min.error")} ${intvlMin}`

    },
    duration: {
      min: `${I18n.get("min.error")} ${durationMin}`,
      max: `${I18n.get("max.error")} ${durationMax}`
    },
    charSet: {
      length: `${I18n.get("string.max.length.error")} ${charSetMax}`
    },
    length: {
      min: `${I18n.get("min.error")} ${lngthMin}`,
      max: `${I18n.get("max.error")} ${lngthMax}`
    }
  }
  return ERRORS[field][error];
}

/**
 * Signs out the user.
 */
export async function signOut() {
  try {
    await Auth.signOut();
    window.location.reload();
  } catch (error) {
    logger.error(I18n.get("sign.out.error"), error);
  }
}
